package com.ruoyi.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basedata.domain.ElectricityAreaZoneJoin;
import com.ruoyi.basedata.domain.ElectricityCollector;
import com.ruoyi.basedata.domain.ElectricityConcentrator;
import com.ruoyi.basedata.mapper.*;
import com.ruoyi.basedata.service.ICollectorService;
import com.ruoyi.basedata.service.IElectricityCollectorService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ElectricityCollectorServiceImpl extends ServiceImpl<ElectricityCollectorMapper, ElectricityCollector> implements IElectricityCollectorService {

    @Autowired
    private ElectricityCollectorMapper collectorMapper;

    @Autowired
    private ElectricityConcentratorMapper concentratorMapper;

    @Override
    public List<ElectricityCollector> getCollectorList(ElectricityCollector collector,Long[] concentratorIds){

        return collectorMapper.getCollectorList(collector,concentratorIds);
    }

    @Override
    public ElectricityCollector getCollectorById(Long id) {
        return collectorMapper.selectById(id);
    }

    @Override
    public Boolean insertCollector(ElectricityCollector collector) {
        int insert = collectorMapper.insert(collector);
        if (insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateCollector(ElectricityCollector collector) {
        int insert = collectorMapper.updateById(collector);
        if (insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteCollector(Long id) {
        int insert = collectorMapper.deleteById(id);
        if (insert>0){
            return true;
        }
        return false;
    }

    @Override
    public List<ElectricityCollector> getCollectorByConcentratorId(Long concentratorId) {
        return collectorMapper.selectList(new LambdaQueryWrapper<ElectricityCollector>().eq(ElectricityCollector::getConcentratorId,concentratorId));
    }

    /**
     * 导入采集器
     *
     * @param list 采集器list
     */
    @Override
    public void importData(List<ElectricityCollector> list) {
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException("没有可导入的数据");
        } else {
            StringBuilder errorMsg = new StringBuilder();
            List<String> allCollectorName = collectorMapper.selectAllCollectorName();
            List<String> allCollectorAddress = collectorMapper.selectAllCollectorAddress();
            Map<String, Integer> collectorNameMap = new HashMap<>(list.size());
            Map<String, Integer> collectorAddressMap = new HashMap<>(list.size());
            for (int i = 0; i < list.size(); i++) {
                ElectricityCollector collector = list.get(i);
                Long concentratorId = collector.getConcentratorId();
                String collectorName = collector.getCollectorName();
                String collectorAddress = collector.getCollectorAdd();
                if (concentratorId == null) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，集中器ID不能为空\n");
                } else {
                    ElectricityConcentrator concentrator = concentratorMapper.selectConcentratorByID(concentratorId);
                    if (concentrator == null) {
                        errorMsg.append("第").append(i + 2).append("行数据错误，集中器ID不存在\n");
                    } else {
                        collector.setTenantId(concentrator.getTenantId());
                    }
                }
                if (StringUtils.isAnyBlank(collectorName, collectorAddress)) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，采集器名称、采集器地址均不能为空\n");
                }
                if (allCollectorName.contains(collectorName)) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，采集器名称已存在\n");
                }
                if (collectorNameMap.get(collectorName) != null) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，").append("采集器名称和第").append(collectorNameMap.get(collectorName) + 2).append("行采集器名称重复\n");
                } else {
                    collectorNameMap.put(collectorName, i);
                }
                if (allCollectorAddress.contains(collectorAddress)) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，采集器地址已存在\n");
                }
                if (collectorAddressMap.get(collectorAddress) != null) {
                    errorMsg.append("第").append(i + 2).append("行数据错误，").append("采集器地址和第").append(collectorAddressMap.get(collectorAddress) + 2).append("行采集器地址重复\n");
                } else {
                    collectorAddressMap.put(collectorAddress, i);
                }
            }
            if (errorMsg.length() == 0) {
                collectorMapper.insertCollectorBatch(list);
            } else {
                throw new ServiceException(errorMsg.toString());
            }
        }
    }


}
